package com.heima.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.heima.ssm.pojo.Orders;
import com.heima.ssm.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Penguin
 * @create Date 2021/5/4  Time 9:58
 */
@Controller
@RequestMapping("orders")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;

    @RequestMapping("findAll")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")int page,
                                @RequestParam(name = "pageSize",required = true,defaultValue = "4")int pageSize){
        ModelAndView mv = new ModelAndView();
        List<Orders> ordersList = ordersService.findAll(page,pageSize);
        PageInfo pageInfo = new PageInfo(ordersList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("orders-list-pagehelper");
        return mv;
    }

    @RequestMapping("findById")
    public ModelAndView findByOrdersId(@RequestParam(name = "id",required = true)int ordersId){
        ModelAndView mv = new ModelAndView();
        Orders orders = ordersService.findByOrdersId(ordersId);
        mv.addObject("orders",orders);
        mv.setViewName("orders-show");
        return mv;
    }
}
